/*
 * This application was developed using and referring to the sample code provided by Sun Microsystems
 * Hence I am including this mandatory copyright notice in order to comply with the rules

 * Copyright (c) 2007 Sun Microsystems, Inc.
 * Copyright (c) 2010 Oracle
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

package ritcs.sensors.app;
import com.sun.spot.io.j2me.radiogram.Radiogram;
import com.sun.spot.resources.Resource;
import java.io.IOException;
import ritcs.sensors.util.PacketTransmitter;
import com.sun.spot.resources.transducers.ITemperatureInput;
import com.sun.spot.resources.transducers.ILightSensor;
import com.sun.spot.resources.transducers.ITriColorLED;
import com.sun.spot.resources.transducers.ITriColorLEDArray;


import ritcs.sensors.util.PeriodicTask;
import com.sun.spot.resources.Resources;
import com.sun.spot.util.Utils;

/**
 *
 * @author chimbs
 */
public class Temperature extends PeriodicTask implements PacketTypes {
PacketTransmitter xmit;
ITriColorLEDArray leds;
ITemperatureInput temp;
ITriColorLED led4;
ILightSensor light;

int network;
    public Temperature(PacketTransmitter xmit, int network){
        super(1,1000);
temp =(ITemperatureInput) Resources.lookup(ITemperatureInput.class);
        leds = (ITriColorLEDArray)Resources.lookup(ITriColorLEDArray.class);
        light =(ILightSensor) Resources.lookup(ILightSensor.class);
        led4 = leds.getLED(4);
this.network = network;
        this.xmit = xmit;
    }
    public void doTask() {

        led4.setRGB(30, 0, 0);

        led4.setOn();


        Radiogram rdg = xmit.newDataPacket(GET_TEMPERATURE_REPLY);
        try {
              rdg.writeDouble(temp.getCelsius());
                    rdg.writeLong(System.currentTimeMillis());
                    rdg.writeInt(network);
                    rdg.writeDouble(light.getAverageValue());
                    xmit.send(rdg);
                  
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

}
